// store/store.js
import Vue from 'vue';
import Vuex from 'vuex';
import videosAboutData from '../js/testData/videosAbout.js'; // 导入视频列表数据
import defaultVideoAbout from '../js/testData/videoAbout.js'; // 导入默认选中的视频数据
import videosPullData from '../js/testData/videosPull.js'; // 导入新的数据

Vue.use(Vuex);

// 从 uniStorage 中获取初始值，或使用导入的默认数据
const savedVideoAbout = uni.getStorageSync('videoAbout') || defaultVideoAbout;
const savedVideosAbout = uni.getStorageSync('videosAbout') || videosAboutData;
const savedVideosPull = uni.getStorageSync('videosPull') || videosPullData;
const savedAnswerContent = uni.getStorageSync('answerContent') || {}; // 获取 answerContent 初始值
const savedVideoOrder = uni.getStorageSync('videoOrder') || []; // 新增 videoOrder 初始值
const savedUserInfo = uni.getStorageSync('userInfo') || {}; // 获取 userInfo 初始值
export default new Vuex.Store({
	state: {
		videoAbout: savedVideoAbout, // 当前选中视频
		videosAbout: savedVideosAbout, // 视频列表
		videosPull: savedVideosPull, // 视频推送数据
		answerContent: savedAnswerContent, // 新增的全局变量
		videoOrder: savedVideoOrder, // 新增的全局变量 videoOrder
		userInfo: savedUserInfo // 新增的全局变量
	},
	mutations: {
		// 更新选中的视频信息
		setVideoAbout(state, newVideoAbout) {
			state.videoAbout = newVideoAbout;
			uni.setStorageSync('videoAbout', newVideoAbout);
		},
		// 更新视频列表
		setVideosAbout(state, newVideosAbout) {
			state.videosAbout = newVideosAbout;
			uni.setStorageSync('videosAbout', newVideosAbout);
		},
		// 更新列表中的某个视频
		updateVideoInVideosAbout(state, updatedVideo) {
			const index = state.videosAbout.findIndex(video => video.videoId === updatedVideo.videoId);
			if (index !== -1) {
				Vue.set(state.videosAbout, index, updatedVideo);
				uni.setStorageSync('videosAbout', state.videosAbout);
			}
		},
		// 更新 videosPull 数据
		setVideosPull(state, newVideosPull) {
			state.videosPull = newVideosPull;
			uni.setStorageSync('videosPull', newVideosPull);
		},
		// 更新 answerContent
		setAnswerContent(state, newAnswerContent) {
			state.answerContent = newAnswerContent;
			uni.setStorageSync('answerContent', newAnswerContent);
		},
		// 更新 videoOrder
		setVideoOrder(state, newVideoOrder) {
			state.videoOrder = newVideoOrder;
			uni.setStorageSync('videoOrder', newVideoOrder);
		},
		// 更新 userInfo
		setUserInfo(state, newUserInfo) {
			state.userInfo = newUserInfo;
			uni.setStorageSync('userInfo', newUserInfo);
		}
	},
	getters: {
		getVideosAbout(state) {
			return state.videosAbout;
		},
		getVideoAbout(state) {
			return state.videoAbout;
		},
		getVideosPull(state) {
			return state.videosPull;
		},
		getAnswerContent(state) {
			return state.answerContent;
		},
		// 获取 videoOrder
		getVideoOrder(state) {
			return state.videoOrder;
		},
		getUserIfo(state) {
			return state.userInfo; // 获取 userInfo
		}
	}
});